// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

export enum RenderType {
    LABEL,
    RICHTEXT,

    SPINE,
    DRAGON,
    SPRITE,

    MOTION_STREAK,
    MASK,
    GRAPHICS,
    PARTICLE_SYSTEM
}

@ccclass
export default class Shader extends cc.Component {
    private _renderType: RenderType = null;

    get renderType(): RenderType {
        return this._renderType;
    }

    private _material: cc.Material = null;

    get material(): cc.Material {
        return this._material;
    }

    set material(material: cc.Material) {
        if (material === this._material) return;
        this._material = material;
        this._renderComponent.setMaterial(0, this._material);
    }

    private _renderComponent: cc.RenderComponent = null;

    get renderComponent(): cc.RenderComponent {
        return this._renderComponent;
    }

    protected onLoad(): void {
        this._renderComponent = this.node.getComponent(cc.RenderComponent);
        if (this._renderComponent) {
            this._material = this._renderComponent.getMaterial(0);
        }
        this.updateRenderType();
    }

    private updateRenderType() {
        if (this._renderComponent) {
            if (this._renderComponent instanceof cc.Label) {
                this._renderType = RenderType.LABEL;
            } else if (this._renderComponent instanceof cc.RichText) {
                this._renderType = RenderType.RICHTEXT;
            } else if (this._renderComponent instanceof sp.Skeleton) {
                this._renderType = RenderType.SPINE;
            } else if (this._renderComponent instanceof dragonBones.ArmatureDisplay) {
                this._renderType = RenderType.DRAGON;
            } else if (this._renderComponent instanceof cc.Sprite) {
                this._renderType = RenderType.SPRITE;
            } else if (this._renderComponent instanceof cc.Mask) {
                this._renderType = RenderType.MASK;
            } else if (this._renderComponent instanceof cc.MotionStreak) {
                this._renderType = RenderType.MOTION_STREAK;
            } else if (this._renderComponent instanceof cc.Graphics) {
                this._renderType = RenderType.GRAPHICS;
            } else if (this._renderComponent instanceof cc.ParticleSystem) {
                this._renderType = RenderType.PARTICLE_SYSTEM;
            }
        }
    }

    protected setProperty(name: string, value: any, passIndex: number = 0, directly?: boolean) {
        if (!this._material) return;
        if (this._material.getProperty(name, passIndex) !== undefined) {
            this._material.setProperty(name, value, passIndex, directly);
        } else {
            cc.warn('当前材质不存在属性：' + name + ',请选择正确的材质');
        }
    }
}
